package com.flower.web.controller.business;

import cn.hutool.core.lang.Snowflake;
import com.flower.common.annotation.Log;
import com.flower.common.core.controller.BaseController;
import com.flower.common.core.domain.AjaxResult;
import com.flower.common.core.page.TableDataInfo;
import com.flower.common.enums.BusinessType;
import com.flower.common.utils.poi.ExcelUtil;
import com.flower.system.domain.BizPeopleUpdates;
import com.flower.system.service.IBizPeopleUpdatesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 个人动态Controller
 * 
 * @author ruoyi
 * @date 2024-04-03
 */
@RestController
@RequestMapping("/system/updates")
public class BizPeopleUpdatesController extends BaseController
{
    @Autowired
    private IBizPeopleUpdatesService bizPeopleUpdatesService;

    /**
     * 查询个人动态列表
     */
    @PreAuthorize("@ss.hasPermi('system:updates:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizPeopleUpdates bizPeopleUpdates)
    {
        startPage();
        List<BizPeopleUpdates> list = bizPeopleUpdatesService.selectBizPeopleUpdatesList(bizPeopleUpdates);
        return getDataTable(list);
    }

    /**
     * 导出个人动态列表
     */
    @PreAuthorize("@ss.hasPermi('system:updates:export')")
    @Log(title = "个人动态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizPeopleUpdates bizPeopleUpdates)
    {
        List<BizPeopleUpdates> list = bizPeopleUpdatesService.selectBizPeopleUpdatesList(bizPeopleUpdates);
        ExcelUtil<BizPeopleUpdates> util = new ExcelUtil<BizPeopleUpdates>(BizPeopleUpdates.class);
        util.exportExcel(response, list, "个人动态数据");
    }

    /**
     * 获取个人动态详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:updates:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(bizPeopleUpdatesService.selectBizPeopleUpdatesById(id));
    }

    /**
     * 新增个人动态
     */
    @PreAuthorize("@ss.hasPermi('system:updates:add')")
    @Log(title = "个人动态", businessType = BusinessType.INSERT)
    @PostMapping({"","/add"})
    public AjaxResult add(@RequestBody BizPeopleUpdates bizPeopleUpdates)
    {
        //添加雪花id
        bizPeopleUpdates.setId(new Snowflake(1,1).nextIdStr());
        //根据token获取登录用户
        bizPeopleUpdates.setUserId(getLoginUser().getUser().getUserId().toString());
        //TODO 追加昵称和头像
        bizPeopleUpdates.setNickName(getLoginUser().getUser().getNickName());
        //标题列用不上，暂时用来存头像
        bizPeopleUpdates.setTitle(getLoginUser().getUser().getAvatar());
        return toAjax(bizPeopleUpdatesService.insertBizPeopleUpdates(bizPeopleUpdates));
    }

    /**
     * 修改个人动态
     */
    @PreAuthorize("@ss.hasPermi('system:updates:edit')")
    @Log(title = "个人动态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPeopleUpdates bizPeopleUpdates)
    {
        return toAjax(bizPeopleUpdatesService.updateBizPeopleUpdates(bizPeopleUpdates));
    }

    /**
     * 删除个人动态
     */
    @PreAuthorize("@ss.hasPermi('system:updates:remove')")
    @Log(title = "个人动态", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bizPeopleUpdatesService.deleteBizPeopleUpdatesByIds(ids));
    }

    /**
     * 查询所有人的动态
     */
    @PreAuthorize("@ss.hasPermi('system:updates:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(BizPeopleUpdates bizPeopleUpdates)
    {
        startPage();
        //什么都不用传

        List<BizPeopleUpdates> list = bizPeopleUpdatesService.selectBizPeopleUpdatesList(bizPeopleUpdates);
        return getDataTable(list);
    }
    /**
     * 查询自己的动态
     */
    @PreAuthorize("@ss.hasPermi('system:updates:list')")
    @GetMapping("/mylist")
    public TableDataInfo listMy(BizPeopleUpdates bizPeopleUpdates)
    {

        //根据token获取登录用户
        bizPeopleUpdates.setUserId(getLoginUser().getUser().getUserId().toString());
        startPage();
        List<BizPeopleUpdates> list = bizPeopleUpdatesService.selectBizPeopleUpdatesList(bizPeopleUpdates);
        return getDataTable(list);
    }
}
